package com.bigegg.employee.api;

import com.bigegg.employee.domain.Activity;
import com.bigegg.employee.domain.College;
import com.bigegg.employee.dto.*;
import com.bigegg.employee.service.QRCodeService;
import com.bigegg.employee.service.impl.JobActivityService;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 给小程序的接口
 */
@RestController
@RequestMapping("/api")
public class ActivityApi extends BaseController {


    @Autowired
    JobActivityService jobActivityService;


    /*---------------公共接口 ---------------------------------

    /**
     * 获取活动列表
     */
    @PostMapping("/activity")
    public AjaxResult getActivityInfo() {

        PageHelper.orderBy("hold_start_time desc");
        List<Activity> activityList = jobActivityService.listHoldingActivity();
        if (activityList != null) {
            return AjaxResult.success(activityList.stream().map(one -> new SimpleActivity(one.getId(), one.getActivityName())).collect(Collectors.toList()));
        }

        return AjaxResult.success();
    }


    /**
     * 活动签到统计
     * 接口提供给学校端和辅导员端，根据请求的用户类型，返回对应用户下的统计，如学校端为学校总人数为纬度统计、辅导员端为所管理的班级总人数为纬度统计
     */
    @PostMapping("/activity_detail")
    public AjaxResult getActivityDetail(@RequestBody ActivityReqBody o) {

        Long activityId = o.getActivity_id();
        return AjaxResult.success(jobActivityService.getActivitySignStat(activityId, getLoginUser()));

    }


    /*---------------学校端 ---------------------------------

    /**
     * 学院签到详情
     */
    @PostMapping("/department_detail")
    public AjaxResult getDepartmentActivityDetail(@RequestBody ActivityReqBody o) {

        Long activityId = o.getActivity_id();

        Long departmentId = o.getDepartment_id();

        return AjaxResult.success(jobActivityService.getDepartmentActivitySignStat(activityId, departmentId, getLoginUser()));
    }

    /**
     * 招聘单位签到详情
     */
    @PostMapping("/unit_detail")
    public AjaxResult getUnitActivityDetail(@RequestBody ActivityReqBody o) {

        Long activityId = o.getActivity_id();

        return AjaxResult.success(jobActivityService.getUnitActivitySignStat(activityId));
    }

    /**
     * 活动签到详细列表
     */
    @PostMapping("/activity_sign_detail")
    public AjaxResult getAllActivityDetail() {

        return AjaxResult.success(jobActivityService.getActivitySignDetail());
    }

    // 查询部门列表
    @PostMapping("/department")
    public AjaxResult getDepartmentList() {
        List<College> colleges = jobActivityService.getDepartmentList();
        if (colleges != null) {
            return AjaxResult.success(colleges.stream().map(a -> new SimpleActivity(a.getId(), a.getName())).collect(Collectors.toList()));
        }
        return AjaxResult.success();
    }



    /*---------------辅导员端 --------------------------------- */


    /**
     * 学生参加活动详情列表
     */
    @PostMapping("/class_detail")
    public AjaxResult getClassDetail(@RequestBody ActClass actClass) {

        return AjaxResult.success(jobActivityService.getClassDetail(actClass,getLoginUser()));
    }


    /**
     * 班级活动统计
     */
    @PostMapping("/class_signed_data")
    public AjaxResult getClassSignedData(@RequestBody ActClass actClass) {
        return AjaxResult.success(jobActivityService.getClassSignedData(actClass,getLoginUser()));
    }

    /**
     * 获取辅导员的班级
     */
    @PostMapping("/class")
    public AjaxResult getClasses() {

        return AjaxResult.success(jobActivityService.getTeacherClasses(getLoginUser()));
    }


    /*---------------学生端 --------------------------------- */

    /**
     * 单位签到
     */
    @PostMapping("/unit_sign")
    public AjaxResult doUnitSign(@RequestBody UnitActSignBody actSignBody) {
        jobActivityService.doUnitSign(actSignBody, getLoginUser());
        return AjaxResult.success();
    }


    /**
     * 单位签到详情
     */

    @PostMapping("/unit_info")
    public AjaxResult getActUnitInfoDetail(@RequestBody UnitActSignBody actSignBody) {
        return AjaxResult.success(jobActivityService.getActUnitInfoDetail(actSignBody, getLoginUser()));
    }

    /**
     * 活动签到
     */
    @PostMapping("/activity_sign")
    public AjaxResult doActivitySign(@RequestBody ActSignBody actSignBody) {
        jobActivityService.doActivitySign(actSignBody, getLoginUser());
        return AjaxResult.success();
    }

    /**
     * 活动签到详情
     */

    @PostMapping("/activity_info")
    public AjaxResult getActInfoDetail(@RequestBody ActSignBody actSignBody) {
        return AjaxResult.success(jobActivityService.getActInfoDetail(actSignBody, getLoginUser()));
    }


    /*---------------单位签到二维码 --------------------------------- */


    @Autowired
    private QRCodeService qrCodeService;

    /**
     * 单位二维码
     */
    @GetMapping("/unit")
    public void getUnitQRCode(@RequestParam Long activity_id, @RequestParam Long unit_id, HttpServletResponse response) {
        OutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
            qrCodeService.generateUnitSignQRCode(activity_id, unit_id, outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 活动二维码
     */
    @GetMapping("/activity")
    public void getActivityQRCode(@RequestParam Long activity_id, HttpServletResponse response) {
        OutputStream outputStream;
        try {
            outputStream = response.getOutputStream();
            qrCodeService.generateActivitySignQRCode(activity_id, outputStream);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
